package com.jmb.api.controller.role;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.Result;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.Role;
import com.jmb.api.service.RoleService;
import com.jmb.api.vo.role.RoleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@Tag(name = "角色管理")
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    // 角色管理新增
    @Operation(summary = "角色管理新增")
    @PostMapping("save")
    public Result addRole(@RequestBody RoleVo roleVo){
        roleService.createRole(roleVo);
        return Result.ok();
    }

    // 角色管理列表
    @Operation(summary = "角色管理列表")
    @GetMapping("list")
    public Result<IPage<RoleVo>> getRoleList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize,RoleVo roleVo){
        IPage<RoleVo> page = new Page<>(pageNum, pageSize);
        IPage<RoleVo>list = roleService.getRoleListByQuery(page,roleVo);
        return Result.ok(list);
    }
    @Operation(summary = "角色管理修改")
    @PostMapping("modify")
    public Result modifyRole(@RequestBody RoleVo roleVo){
        roleService.updateRole(roleVo);
        return Result.ok();
    }
    // 提供用户管理使用
    @Operation(summary = "角色列表")
    @GetMapping("all")
    public Result<List<Role>> getAllRole(@RequestParam Integer status){
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus,status);
        List<Role> list = roleService.list(queryWrapper);
        return Result.ok(list);
    }
    @Operation(summary = "角色管理删除")
    @GetMapping("remove")
    public Result removeRole(@RequestParam Integer id){
        if(id == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        roleService.removeById(id);
        return Result.ok();
    }
}
